Skip to content

[CODE HEALTH] Fix clang-tidy bugprone-exception-escape warnings in API#3964

Open
dbarker wants to merge 9 commits intoopen-telemetry:mainfrom
dbarker:fix_clangtidy_exception_escape_in_api
Open

[CODE HEALTH] Fix clang-tidy bugprone-exception-escape warnings in API#3964
dbarker wants to merge 9 commits intoopen-telemetry:mainfrom
dbarker:fix_clangtidy_exception_escape_in_api

Conversation

@dbarker
Copy link
Copy Markdown
Member

@dbarker dbarker commented Apr 1, 2026

Contributes to #2053

Changes

  • Add macros to support try/catch when compiling with and without exceptions
  • Add macro for unreachable code paths
  • Add unit test coverage for cases with string_view, string_util, and trace state.
  • Fixes the following warnings:

bugprone-exception-escape (4 warnings)

File Line Message
opentelemetry-cpp/api/include/opentelemetry/trace/trace_state.h 55 an exception may be thrown in function 'FromHeader' which should not throw exceptions
opentelemetry-cpp/api/include/opentelemetry/trace/trace_state.h 118 an exception may be thrown in function 'Get' which should not throw exceptions
opentelemetry-cpp/api/include/opentelemetry/trace/trace_state.h 138 an exception may be thrown in function 'Set' which should not throw exceptions
opentelemetry-cpp/api/include/opentelemetry/trace/trace_state.h 172 an exception may be thrown in function 'Delete' which should not throw exceptions

For significant contributions please make sure you have completed the following items:

  • CHANGELOG.md updated for non-trivial changes
  • Unit tests have been added
  • Changes in public API reviewed

@codecov
Copy link
Copy Markdown

codecov bot commented Apr 1, 2026

Codecov Report

❌ Patch coverage is 82.75862% with 10 lines in your changes missing coverage. Please review.
✅ Project coverage is 90.05%. Comparing base (813089c) to head (79eed4e).

Files with missing lines Patch % Lines
api/include/opentelemetry/trace/trace_state.h 84.32% 8 Missing ⚠️
api/include/opentelemetry/plugin/detail/utility.h 50.00% 2 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #3964      +/-   ##
==========================================
- Coverage   90.13%   90.05%   -0.08%     
==========================================
  Files         230      230              
  Lines        7293     7304      +11     
==========================================
+ Hits         6573     6577       +4     
- Misses        720      727       +7     
Files with missing lines Coverage Δ
api/include/opentelemetry/common/string_util.h 100.00% <100.00%> (ø)
api/include/opentelemetry/nostd/string_view.h 98.15% <ø> (ø)
api/include/opentelemetry/nostd/variant.h 66.67% <ø> (ø)
api/include/opentelemetry/plugin/detail/utility.h 50.00% <50.00%> (ø)
api/include/opentelemetry/trace/trace_state.h 90.33% <84.32%> (-7.29%) ⬇️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Copy Markdown
Member

@marcalff marcalff left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change to replace nostd::holds_alternative with nostd::get_if is clean and a no brainer.

Please prepare a separate patch to fix all nostd::holds_alternative so this part can be merged out of the way.

This PR can then focus on the try/catch code alone, as it will be more tricky to resolve.

# define OPENTELEMETRY_UNREACHABLE() __assume(0)
#else
# include <cstdlib>
# define OPENTELEMETRY_UNREACHABLE() abort()
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the fallback abort or something else?

if (pos > length_)
{
# if __EXCEPTIONS
# if OPENTELEMETRY_HAVE_EXCEPTIONS
Copy link
Copy Markdown
Member Author

@dbarker dbarker Apr 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The behavior seems to have been inconsistent in the past between compilers (msvc is not setting __EXCEPTIONS). Changing to use the common macro seems reasonable and expected here, but is a change of behavior on msvc (which would now compile with this throw for string_view unless exceptions are disabled).

@dbarker dbarker marked this pull request as ready for review April 3, 2026 13:30
@dbarker dbarker requested a review from a team as a code owner April 3, 2026 13:30
@dbarker
Copy link
Copy Markdown
Member Author

dbarker commented Apr 3, 2026

The change to replace nostd::holds_alternative with nostd::get_if is clean and a no brainer.

Please prepare a separate patch to fix all nostd::holds_alternative so this part can be merged out of the way.

This PR can then focus on the try/catch code alone, as it will be more tricky to resolve.

Thanks @marcalff. I've split this up. The nostd::variant access changes are now in #3965. Interested in feedback on the try/catch/unreachable macros given our need to support noexcept builds and analyzers.

{
return str.substr(left, 1 + right - left);
}
OPENTELEMETRY_CATCH_ALL
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once we start to add these to the sdk we may want to expand these macros to support passing information to the logger (exception message and other context from the call site).

OPENTELEMETRY_TRY
{
return TraceState::GetDefault();
if (!IsValidKey(key))
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One alternative to consider is to make IsValidKey/IsValidValue noexcept and put the try catch in those methods. That is an API impact but perhaps not an ABI change. The std::regex ctor and std::regex_match may throw.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants